<?php
	//权限角色类
	class role
	{
		var $database = null;

		function role()
		{
			$this->database = load( 'database' );
		}

		//返回角色列表数据
		function all( $data )
		{
			$size = isset( $data['size'] ) && preg_match( '!^\d+$!', $data['size'] ) ? $data['size'] : 12;
			$page = isset( $data['page'] ) && preg_match( '!^\d+$!', $data['page'] ) ? $data['page'] : 1;
			$order = isset( $data['order'] ) ? $data['order'] : 'id asc';
			$count = 'from role';
			$list = 'select * ' . $count . ' order by ' . $order;
			$result = $this->database->split( $count, $list, $size, $page );
			return $result;
		}

		//返回某角色数据
		function get( $data )
		{
			$result = array();

			if ( isset( $data['id'] ) && preg_match( '!^\d+$!', $data['id'] ) )
			{
				$result = $this->database->unique( 'select * from role where id = ' . $data['id'] );

				if ( isset( $result['id'] ) )
				{
					$temp = $this->database->select( 'select privilege_id from role_privilege where role_id = ' . $data['id'] );
					$result['privilege_id_array'] = array();
					foreach( $temp as $value ) $result['privilege_id_array'][$value['privilege_id']] = '';
				}
			}

			return $result;
		}

		//修改某角色数据
		function set( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$check = array();

			if ( ! isset( $data['name'] ) || strlen( $data['name'] ) < 1 ) $check[] = '请输入名称';
			if ( ! isset( $data['id'] ) || ! preg_match( '!^\d+$!', $data['id'] ) ) $check[] = '请传递角色ID';

			if ( count( $check ) == 0 )
			{
				$privilege_id_array = isset( $data['privilege_id'] ) ? $data['privilege_id'] : array();
				unset( $data['privilege_id'] );

				$this->database->start();
				$this->database->set( 'role', $data, array( 'id' => $data['id'] ) );

				$this->database->command( 'delete from role_privilege where role_id = ' . $data['id'] );
				foreach( $privilege_id_array as $privilege_id ) if ( preg_match( '!^\d+$!', $privilege_id ) ) $this->database->add( 'role_privilege', array( 'privilege_id' => $privilege_id, 'role_id' => $data['id'] ) );

				if ( $this->database->end() )
				{
					$result['success'] = true;
					$result['data']['id'] = $data['id'];
				}
				else
				{
					$result['message'][] = '执行修改失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}

		//添加新角色
		function add( $data )
		{
			$result = array( 'success' => false, 'message' => array() );
			$check = array();

			if ( ! isset( $data['name'] ) || strlen( $data['name'] ) < 1 ) $check[] = '请输入名称';

			if ( count( $check ) == 0 )
			{
				$privilege_id_array = isset( $data['privilege_id'] ) ? $data['privilege_id'] : array();
				unset( $data['privilege_id'] );

				$this->database->start();
				$id = $this->database->add( 'role', $data );

				if ( preg_match( '!^\d+$!', $id ) )
				{
					foreach( $privilege_id_array as $privilege_id ) if ( preg_match( '!^\d+$!', $privilege_id ) ) $this->database->add( 'role_privilege', array( 'privilege_id' => $privilege_id, 'role_id' => $id ) );

					if ( $this->database->end() )
					{
						$result['success'] = true;
						$result['data']['id'] = $id;
					}
					else
					{
						$result['message'][] = '执行创建角色权限失败';
					}
				}
				else
				{
					$result['message'][] = '执行创建失败';
				}
			}
			else
			{
				$result['message'] = $check;
			}

			return $result;
		}
	}
?>